package com.mycat.controller;

import com.mycat.util.SessionUtil;
import com.mycat.mapper.ItemMapper;
import com.mycat.pojo.Item;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
public class TestController {

    @Autowired
    ItemMapper itemMapper;

    @GetMapping("get")
    @ResponseBody
    public Object get(int id){
        System.out.println(id);
        return itemMapper.getOne(id);
    }

    @GetMapping("getList")
    @ResponseBody
    public List<Item> getList(){
        return itemMapper.getList();
    }

    //简化，直接通过这里设置session
    @GetMapping("/set/{sess}")
    @ResponseBody
    public Object setSession(@PathVariable("sess") String sess){
        HttpSession httpSession=SessionUtil.getSession();
        httpSession.setAttribute("corp",sess);
        return "ok";
    }

    @GetMapping("geCurrentSession")
    @ResponseBody
    public String geCurrentSession(){
        return SessionUtil.getSession().getAttribute("corp").toString();
    }

    @GetMapping("/count")
    @ResponseBody
    public Object getCount(){
        //要测试的方法

        return itemMapper.count();
    }

    @PostMapping("/add")
    @ResponseBody
    public Object add(@RequestBody Item item){
        if(StringUtils.isEmpty(item.getIndate())){
            item.setIndate(new Date());
        }
        itemMapper.insert(item);
        return 1;
    }

}
